﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace CdhpyScheduler.Core
{
    public enum JobModel_IntervalType
    {
        每间隔一段时间 = 1,
        每天 = 2
    }

    public class IJobModel : ICloneable
    {
        string jobKey_name { get; set; }

        string jobKey_group { get; set; }

        string trigger_name { get; }

        JobModel_IntervalType interval_type { get; set; }

        int interval_days { get; set; }

        int interval_hours { get; set; }

        int interval_minutes { get; set; }

        int interval_seconds { get; set; }

        /// <summary>
        /// unit: seconds
        /// </summary>
        int interval { get; }

        string url { get; set; }

        string http_method { get; set; }

        int repeate_count { get; set; }

        string remark { get; set; }

        public virtual object Clone()
        {
            return default(IJobModel);
        }
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class JobModel : IJobModel
    {
        public string jobKey_name { get; set; }

        public string jobKey_group { get; set; }

        [JsonIgnore]
        public string trigger_name { get { return "trig_" + jobKey_name; } }

        public JobModel_IntervalType interval_type { get; set; }

        public int interval_days { get; set; }

        public int interval_hours { get; set; }

        public int interval_minutes { get; set; }

        public int interval_seconds { get; set; }

        /// <summary>
        /// unit: seconds
        /// </summary>
        [JsonIgnore]
        public int interval
        {
            get
            {
                return ((interval_days * 24 + interval_hours) * 60 + interval_minutes) * 60 + interval_seconds;
            }
        }

        public string url { get; set; }

        public string http_method { get; set; }

        public bool isTemp { get; set; }

        public int repeate_count { get; set; }

        public string remark { get; set; }

        /// <summary>
        /// Creates a new object that is a copy of the current instance
        /// </summary>
        /// <returns>A new object that is a copy of this instance</returns>
        public new virtual object Clone()
        {
            JobModel copy;
            try
            {
                copy = (JobModel)MemberwiseClone();
            }
            catch (Exception)
            {
                throw new Exception("Not Cloneable!");
            }
            return copy;
        }

        public override int GetHashCode()
        {
            return (this.jobKey_name + this.jobKey_group).GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
                return false;

            var that = (JobModel)obj;

            if (this.http_method?.ToLower() != that.http_method?.ToLower())
                return false;
            if (this.jobKey_group?.ToLower() != that.jobKey_group?.ToLower())
                return false;
            if (this.jobKey_name?.ToLower() != that.jobKey_name?.ToLower())
                return false;
            if (this.url?.ToLower() != that.url?.ToLower())
                return false;
            if (this.interval != that.interval)
                return false;
            if(this.interval_days != that.interval_days)
                return false;
            if(this.interval_hours != that.interval_hours)
                return false;
            if(this.interval_minutes != that.interval_minutes)
                return false;
            if(this.interval_seconds != that.interval_seconds)
                return false;
            if(this.interval_type != that.interval_type)
                return false;
            if (this.repeate_count != that.repeate_count)
                return false;

            return true;
        }
    }
}
